package test;
public class GridNine {
//main メッソド
public static void main(String[] args) {
int[] nineGrid = new int[9];
printGridNine(nineGrid, 0);
}
//計算 メッソド
public static void printGridNine(int[] nineGrid, int length) {
//1..9の数字をloopして、GridNineに1..9の数字を追加する
for(int i = 1; i <= 9; i ++) {
//存在するかどうかチェック処理を実行する
boolean isExist = false;
for(int k = 0; k < length; k ++) {
if(nineGrid[k] == i) {
isExist = true;
break;
}
}
//存在
if(isExist) {
continue;
}
//存在しない
else {
//1..9の数字を追加する
nineGrid[length] = i;
if(length == 9 - 1) {
//表示
pringGridNine(nineGrid);
} else {
//追加処理を続行する
printGridNine(nineGrid, length + 1);
}
}
}
}
//表示 メッソド
public static void pringGridNine(int[] nineGrid) {
if(nineGrid[0] + nineGrid[1] + nineGrid[2] == 15
&& nineGrid[3] + nineGrid[4] + nineGrid[5] == 15
&& nineGrid[6] + nineGrid[7] + nineGrid[8] == 15
&& nineGrid[0] + nineGrid[4] + nineGrid[8] == 15
&& nineGrid[2] + nineGrid[4] + nineGrid[6] == 15
&& nineGrid[0] + nineGrid[3] + nineGrid[6] == 15
&& nineGrid[1] + nineGrid[4] + nineGrid[7] == 15
&& nineGrid[2] + nineGrid[5] + nineGrid[8] == 15) {
for(int m = 0; m < nineGrid.length; m ++) {
if((m + 1) % 3 == 0) {
System.out.print(nineGrid[m]);
System.out.println();
} else {
System.out.print(nineGrid[m] + ",");
}
}
System.out.println();
}
}
}